package org.example.StudentManagementSystem;

import java.time.LocalDate;
import java.util.Random;
import java.util.Scanner;

public class StudentManagementSystem {
    Scanner sc = new Scanner(System.in);
    private Student[] students;
    private JuniorStudent[] juniorStudents;

    // 1.添加学生信息的通用方法
    public void addStudent(String studentType, int num) {
        if (studentType.equals("小学生")) {
            students = new Student[num];
            for (int i = 0; i < students.length; i++) {
                students[i] = new Student();

                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的学号：");
                students[i].setStudentId(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的姓名：");
                students[i].setStudentName(sc.next());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的语文成绩：");
                students[i].setChinese(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的数学成绩：");
                students[i].setMath(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的入学日期：（格式：yyyy-MM-dd）");
                String enrollDate = sc.next();
                students[i].setEnrollmentDate(LocalDate.parse(enrollDate));
                boolean checkMail = false;
                while (!checkMail) {
                    System.out.print("请输入第" + (i + 1) + "个" + studentType + "的邮箱：");
                    students[i].setMail(sc.next());
                    if (students[i].getMail().matches("\\w+@\\w+\\.\\w+")) {
                        System.out.println("邮箱录入成功");
                        checkMail = true;
                    } else {
                        System.out.println("邮箱格式错误，请重新录入：");
                        checkMail = false;
                    }
                }
                System.out.println("已录入第" + (i + 1) + "个" + studentType + "的信息：\n");
            }
        } else if (studentType.equals("中学生")) {
            juniorStudents = new JuniorStudent[num];
            for (int i = 0; i < juniorStudents.length; i++) {
                juniorStudents[i] = new JuniorStudent();
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的学号：");
                juniorStudents[i].setStudentId(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的姓名：");
                juniorStudents[i].setStudentName(sc.next());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的语文成绩：");
                juniorStudents[i].setChinese(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的数学成绩：");
                juniorStudents[i].setMath(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的物理成绩：");
                juniorStudents[i].setPhysics(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的历史成绩：");
                juniorStudents[i].setHistory(sc.nextInt());
                System.out.print("请输入第" + (i + 1) + "个" + studentType + "的入学日期：（格式：yyyy-MM-dd）");
                String enrollDate = sc.next();
                juniorStudents[i].setEnrollmentDate(LocalDate.parse(enrollDate));
                boolean checkMail = false;
                while (!checkMail) {
                    System.out.print("请输入第" + (i + 1) + "个" + studentType + "的邮箱：");
                    juniorStudents[i].setMail(sc.next());
                    if (juniorStudents[i].getMail().matches("\\w+@\\w+\\.\\w+")) {
                        System.out.println("邮箱录入成功");
                        checkMail = true;
                    } else {
                        System.out.println("邮箱格式错误，请重新录入：");
                        checkMail = false;
                    }
                }
                System.out.println("已录入第" + (i + 1) + "个" + studentType + "的信息：\n");

            }
        }
    }

    //添加中学生信息
    public void addJuniorStudent() {
        System.out.println("请输入中学生总数");
        int num = sc.nextInt();
        addStudent("中学生", num);
    }

    //添加小学生信息
    public void addPrimaryStudent() {
        System.out.println("请输入小学生总数");
        int num = sc.nextInt();
        addStudent("小学生", num);
    }

    //2.展示学生信息：
    public void showAllStudent() {
        System.out.println("---------所有小学生信息----------");
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (students[i] != null) {
                System.out.println((i + 1) + " " + student.toString());
            }
        }
        System.out.println("----------获取小学生中数学最高成绩的学生---------");
        int maxMathIdex = 0;
        for (int i = 0; i < students.length; i++) {
            if (students[i].getMath() > students[maxMathIdex].getMath()) {
                maxMathIdex = i;
            }
        }
        System.out.println(students[maxMathIdex].toString());
        System.out.println("----------获取小学生中语文最低成绩的学生---------");
        int minChineseIdex = 0;
        for (int i = 0; i < students.length; i++) {
            if (students[i].getChinese() < students[minChineseIdex].getChinese()) {
                minChineseIdex = i;
            }
        }
        System.out.println(students[minChineseIdex].toString());
        System.out.println("---------所有中学生信息----------");
        for (int i = 0; i < juniorStudents.length; i++) {
            JuniorStudent juniorStudent = juniorStudents[i];
            if (juniorStudents[i] != null) {
                System.out.println((i + 1) + " " + juniorStudent.toString());
            }
        }
        System.out.println("----------获取初中生中物理最高成绩的学生---------");
        int maxPhysicsIdex = 0;
        for (int i = 0; i < juniorStudents.length; i++) {
            if (juniorStudents[i].getPhysics() > juniorStudents[i].getPhysics()) {
                maxPhysicsIdex = i;
            }
        }
        System.out.println(juniorStudents[maxPhysicsIdex].toString());
        System.out.println("----------获取初中生中历史最低成绩的学生---------");
        int minHistoryIdex = 0;
        for (int i = 0; i < juniorStudents.length; i++) {
            if (juniorStudents[i].getHistory() < juniorStudents[i].getHistory()) {
                minHistoryIdex = i;
            }
        }
        System.out.println(juniorStudents[minHistoryIdex].toString());
    }

    //3.获取中小学生单科成绩的平均分
    public void getAverageScore() {
        System.out.println("请输入要获取的单科科目：");
        String subject = sc.next();
        System.out.println(("请输入要查询的学生类型："));
        String studenType = sc.next();
        if (subject.equals("数学") && studenType.equals("小学生")) {
            System.out.println("----------小学生数学平均成绩-----------");
            int addPrimaryMath = 0;
            for (int i = 0; i < students.length; i++) {
                addPrimaryMath = addPrimaryMath + students[i].getMath();
            }
            int avgPrimaryMath = addPrimaryMath / students.length;
            System.out.println("小学生数学平均成绩：" + avgPrimaryMath);
        } else if (subject.equals("语文") && studenType.equals("小学生")) {
            System.out.println("----------小学生语文平均成绩-----------");
            int addPrimaryChinese = 0;
            for (int i = 0; i < students.length; i++) {
                addPrimaryChinese = addPrimaryChinese + students[i].getChinese();
            }
            int avgPrimaryChinese = addPrimaryChinese / students.length;
            System.out.println("小学生语文平均成绩：" + avgPrimaryChinese);
        } else if (subject.equals("数学") && studenType.equals("中学生")) {
            System.out.println("----------初中生数学平均成绩-----------");
            int addJuniorMath = 0;
            for (int i = 0; i < juniorStudents.length; i++) {
                addJuniorMath = addJuniorMath + juniorStudents[i].getMath();
            }
            int avgJuniorMath = addJuniorMath / juniorStudents.length;
            System.out.println("中学生的数学平均成绩：" + avgJuniorMath);
        } else if (subject.equals("语文") && studenType.equals("中学生")) {
            System.out.println("----------初中生语文平均成绩-----------");
            int addJuniorChinese = 0;
            for (int i = 0; i < juniorStudents.length; i++) {
                addJuniorChinese = addJuniorChinese + juniorStudents[i].getChinese();
            }
            int avgJuniorChinese = addJuniorChinese / juniorStudents.length;
            System.out.println("中学生的语文平均成绩：" + avgJuniorChinese);
        } else if (subject.equals("物理")) {
            System.out.println("----------初中生物理平均成绩-----------");
            int addJuniorPhysics = 0;
            for (int i = 0; i < juniorStudents.length; i++) {
                addJuniorPhysics = addJuniorPhysics + juniorStudents[i].getPhysics();
            }
            int avgJuniorPhysics = addJuniorPhysics / juniorStudents.length;
            System.out.println("中学生的物理平均成绩：" + avgJuniorPhysics);
        } else if (subject.equals("历史")) {
            System.out.println("----------初中生历史平均成绩-----------");
            int addJuniorHistory = 0;
            for (int i = 0; i < juniorStudents.length; i++) {
                addJuniorHistory = addJuniorHistory + juniorStudents[i].getHistory();
            }
            int avgJuniorHistory = addJuniorHistory / juniorStudents.length;
            System.out.println("中学生的历史平均成绩：" + avgJuniorHistory);
        } else {
            System.out.println("输入的学生类型或科目不符，请重新输入");
        }

    }

    //4.按照学生总成绩进行排名
    public void sortByTotalScore() {
        System.out.println("--------------小学生成绩排名---------------");
        if (students != null && students.length > 0) {
            for (int i = 0; i < students.length - 1; i++) {
                for (int j = 0; j < students.length - 1 - i; j++) {
                    if (students[j].getTotalScore() < students[j + 1].getTotalScore()) {
                        // 交换两个学生对象
                        Student temp = students[j];
                        students[j] = students[j + 1];
                        students[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < students.length; i++) {
                System.out.println((i + 1) + " " + students[i].toString());
            }
        }

        System.out.println("--------------初中生成绩排名---------------");
        if (juniorStudents != null && juniorStudents.length > 0) {
            for (int i = 0; i < juniorStudents.length - 1; i++) {
                for (int j = 0; j < juniorStudents.length - 1 - i; j++) {
                    if (juniorStudents[j].getTotalScore() < juniorStudents[j + 1].getTotalScore()) {
                        // 交换两个学生对象
                        JuniorStudent temp = juniorStudents[j];
                        juniorStudents[j] = juniorStudents[j + 1];
                        juniorStudents[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < juniorStudents.length; i++) {
                System.out.println((i + 1) + " " + juniorStudents[i].toString());
            }
        }
    }

    //根据学号查找学生
    public void selectByStudentId() {
        System.out.println("-----------------查找学生--------------------------");
        boolean find = false;
        while (!find) {
            System.out.println("请输入要查找学生的学号：");
            int findID = sc.nextInt();
            int findIndex = -1;
            for (int i = 0; i < students.length; i++) {
                if (students[i].getStudentId() == findID) {
                    findIndex = i;
                    break;
                }
            }
            if (findIndex != -1) {
                System.out.println("-----------------查询结果（小学生）--------------------------");
                System.out.println(students[findIndex].toString());
                break;
            }
            for (int i = 0; i < juniorStudents.length; i++) {
                if (juniorStudents[i].getStudentId() == findID) {
                    findIndex = i;
                    break;
                }
            }

            if (findIndex != -1) {
                System.out.println("-----------------查询结果（中学生）--------------------------");
                System.out.println(juniorStudents[findIndex].toString());
                break;
            }
            if (!find) {
                System.out.println("未查找到该学生");
            }
            System.out.println("是否继续查找？（y/n）");
            String choice = sc.next();
            if (choice.equals("y")) {
                find = false;
            } else if (choice.equals("n")) {
                System.out.println("退出查找。");
                find = true;
            }
        }
    }

    //查询指定日期之前和之后的小学生
    public void getPrimaryStudentdEntrolledbBefore() {
        Student[] beforetemp = new Student[students.length];
        Student[] aftertemp = new Student[students.length];
        System.out.println("请输入中学生指定日期：");
        String Date = sc.next();
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getEnrollmentDate() != null && student.getEnrollmentDate().isBefore(LocalDate.parse(Date))) {

                beforetemp[i] = student;
            } else if (student != null && student.getEnrollmentDate() != null && student.getEnrollmentDate().isAfter(LocalDate.parse(Date))) {
                aftertemp[i] = student;
            }
        }

        System.out.println("指定日期之前入学的小学生:");
        for (int i = 0; i < beforetemp.length; i++) {
            if (beforetemp[i] != null) {
                System.out.println(beforetemp[i].toString());
            }
        }
        System.out.println("指定日期之前入学的小学生:");
        for (int i = 0; i < aftertemp.length; i++) {
            if (aftertemp[i] != null) {
                System.out.println(aftertemp[i].toString());
            }
        }
    }

    //查询指定日期之前和之后的中学生
    public void getJuniorStudentdEntrolledbAfter() {
        JuniorStudent[] aftertemp = new JuniorStudent[juniorStudents.length];
        JuniorStudent[] beforetemp = new JuniorStudent[juniorStudents.length];
        System.out.println("请输入小学生指定日期：");
        String Date = sc.next();
        for (int i = 0; i < juniorStudents.length; i++) {
            JuniorStudent student = juniorStudents[i];
            if (student != null && student.getEnrollmentDate() != null && student.getEnrollmentDate().isBefore(LocalDate.parse(Date))) {

                beforetemp[i] = student;
            } else if (student != null && student.getEnrollmentDate() != null && student.getEnrollmentDate().isAfter(LocalDate.parse(Date))) {
                aftertemp[i] = student;
            }
        }

        System.out.println("指定日期之前入学的中学生:");
        for (int i = 0; i < beforetemp.length; i++) {
            if (beforetemp[i] != null) {
                System.out.println(beforetemp[i].toString());
            }
        }
        System.out.println("指定日期之前入学的中学生:");
        for (int i = 0; i < aftertemp.length; i++) {
            if (aftertemp[i] != null) {
                System.out.println(aftertemp[i].toString());
            }
        }
    }

    public void randomRollCall() {
        StringBuilder studentName = new StringBuilder();
        for (int i = 0; i < students.length; i++) {
            studentName.append(students[i].getStudentName());
        }
        Random r = new Random();
        for (int i = 0; i < 1; i++) {
            int index = r.nextInt(students.length);
            System.out.println("随机点名：\n" + students[index]);
        }
    }
}
